﻿@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="GameRecordOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" Title="@($"{_name}列表")" ShowAddButton="false" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <div style="width: 170px;">
                    @if (context.Item.ShowPublicly)
                    {
                        <MIcon Color="success"
                               Class="mr-2"
                               OnClick="()=>ShowPubliclyItem(context.Item,false)">@IconType.Link.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>ShowPubliclyItem(context.Item,true)">@IconType.UnLink.ToIconString()</MIcon>
                    }
                    @if (context.Item.IsHidden)
                    {
                        <MIcon Color="success"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,false)">@IconType.Check.ToIconString()</MIcon>
                    }
                    else
                    {
                        <MIcon Color="error"
                               Class="mr-2"
                               OnClick="()=>HideItem(context.Item,true)">@IconType.Close.ToIconString()</MIcon>
                    }

                </div>
            }
            else if (context.Header.Value == nameof(GameRecordOverviewModel.IsHidden))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Recommends.StateChip IsHidden="context.Item.IsHidden" />
            }
            else if (context.Header.Value == nameof(GameRecordOverviewModel.GameName))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Users.Certifications.EntryChip Id="@context.Item.GameId" Name="@context.Item.GameName" Type="EntryType.Game" />
            }
            else if (context.Header.Value == nameof(GameRecordOverviewModel.UserName))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.EditRecords.UserCenter.ReviewEditRecords.UserChip Id="@context.Item.UserId" Name="@context.Item.UserName" />
            }
            else if (context.Header.Value == nameof(GameRecordOverviewModel.ShowPublicly))
            {
                <ShowPubilcChip ShowPublicly="context.Item.ShowPublicly" />
            }
            else if (context.Header.Value == nameof(GameRecordOverviewModel.Type))
            {
                @context.Item.Type.GetDisplayName()
            }
            else if (context.Header.Value == nameof(GameRecordOverviewModel.PlayImpressions))
            {
                <div style="white-space: pre-line;width: 300px;word-break: break-all;">
                    @context.Item.PlayImpressions
                </div>
            }
            else if (context.Header.Value == nameof(GameRecordOverviewModel.LastEditTime))
            {
                @context.Item.LastEditTime.ToString("yyyy-MM-dd HH:mm")
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>


@code {
    private string _baseUrl = "api/playedgame/List";
    private string _name = "游玩记录";

    private List<DataTableHeader<GameRecordOverviewModel>> _headers = new List<DataTableHeader<GameRecordOverviewModel>>
{
          new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(GameRecordOverviewModel.Id)
          },
          new (){ Text= "类型", Value= nameof(GameRecordOverviewModel.Type)},
          new (){ Text= "游戏", Value= nameof(GameRecordOverviewModel.GameName)},
          new (){ Text= "用户", Value= nameof(GameRecordOverviewModel.UserName)},
          new (){ Text= "评语", Value= nameof(GameRecordOverviewModel.PlayImpressions)},
          new (){ Text= "总分", Value= nameof(GameRecordOverviewModel.TotalSocre)},
          new (){ Text= "最后编辑时间", Value= nameof(GameRecordOverviewModel.LastEditTime)},
          new (){ Text= "状态", Value= nameof(GameRecordOverviewModel.IsHidden)},
          new (){ Text= "公开", Value= nameof(GameRecordOverviewModel.ShowPublicly)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<GameRecordOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public async Task HideItem(GameRecordOverviewModel model, bool isHidden)
    {
        string operationName = (isHidden ? "隐藏" : "显示")+_name;
        try
        {
            var obj = await _httpService.PostAsync<HiddenGameRecordModel, Result>("api/playedgame/HiddenGameRecord", new HiddenGameRecordModel { PlayedGameIds = new long[] { model.Id }, IsHidden = isHidden });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败", AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }

    public async Task ShowPubliclyItem(GameRecordOverviewModel model, bool show)
    {
        string operationName = (show ? "公开" : "隐藏") + _name;
        try
        {
            var obj = await _httpService.PostAsync<HiddenGameRecordModel, Result>("api/playedgame/ShowPubliclyGameRecord", new HiddenGameRecordModel { PlayedGameIds = new long[] { model.Id }, IsHidden = show });

            if (obj.Successful == false)
            {
                await PopupService.ToastAsync(operationName + "失败", AlertTypes.Error);
            }
            else
            {

                await PopupService.ToastAsync(operationName + "成功", AlertTypes.Success);
                dataTableCard.GetDataFromApi();

            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, operationName + "失败");
        }
    }
}
